//
//  HECaseTypeCell.m
//  HealthElderly
//
//  Created by 吴欧 on 2021/6/18.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HECaseTypeCell.h"
 
#import "UICollectionViewLeftAlignedLayout.h"
#import "HEHealthHisItemCell.h"

@interface HECaseTypeCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, assign) CGFloat height;
@end

@implementation HECaseTypeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.width = KScreenWidth;
         // Fix the bug in iOS7 - initial constraints warning
         self.contentView.bounds = [UIScreen mainScreen].bounds;
        [self initUI];
    }
    return self;
}

- (void)initUI {
    UICollectionViewLeftAlignedLayout *layout = [[UICollectionViewLeftAlignedLayout alloc] init];
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.alwaysBounceVertical = YES;
    [self.contentView addSubview:_collectionView];
    [self.collectionView registerClass:[HEHealthHisItemCell class] forCellWithReuseIdentifier: KHEHealthHisItemCell];
    
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(5);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
        make.height.mas_equalTo(50).priorityLow();//设置一个高度，以便赋值后更新
        make.bottom.equalTo(self.contentView).mas_offset(-15);
    }];
}

- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath {

    self.fiterModel = fiterModel;
  
    [_collectionView reloadData];
    /** 强制视图立即更新其布局，即同步执行 立即获取collectionView的高度*/
    [_collectionView layoutIfNeeded];
    
    //获取collectionview高度
    //更新collectionView
    _height = [fiterModel getCollectionHeight: _collectionView];
    [_collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(_height).priorityMedium();
    }];
}
 
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    NSArray *secs = self.fiterModel.cellData;
    return secs.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSArray *secs = self.fiterModel.cellData;
    NSArray *rows = secs[section];
    return rows.count;
}
 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HEHealthHisItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: KHEHealthHisItemCell forIndexPath:indexPath];
    NSArray *secs = self.fiterModel.cellData;
    NSArray *rows = secs[indexPath.section];
    cell.model =  rows[indexPath.item];

    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *secs = self.fiterModel.cellData;
    NSArray *rows = secs[indexPath.section];
    HEHealthHisItemModel *model = rows[indexPath.item];
    
    model.isSelect = !model.isSelect;
    [_collectionView reloadData];
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *secs = self.fiterModel.cellData;
    NSArray *rows = secs[indexPath.section];
    HEHealthHisItemModel *model = rows[indexPath.item];
    NSString *string = model.title;
    
    CGSize size = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options: NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: DYFont(16)} context:nil].size;
    
    CGFloat width = size.width + 15 * 2;
    
    if (width >= (KScreenWidth-40)) {
        width = KScreenWidth-40;
    }
    
    CGSize rSize = CGSizeMake(width, 34);
    
    return rSize;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(15, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10.f;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 10.f;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake(kScreenWidth, 10);
}


@end
